package sys;

import java.util.*;

import sys.fsm.*;
import sys.transport.*;
import sys.logic.*;
import sys.message.*;
import sys.timer.TimerClient;
import sys.database.*;
import java.util.concurrent.*;

public class Main
{
    Timer timer;

    public static void main(String args[])
    {
        if (args.length > 0)
        {
            if (args[0].compareTo("system") == 0)
            {
                System.out.println("Starting up a system...");
            }
            else if (args[0].compareTo("LogicTests") == 0)
            {
                System.out.println("Runnig logic tests");
                sys.tests.logic.RunTests.run();
            }
            else if (args[0].compareTo("AutomatTests") == 0)
            {
                System.out.println("Running automat tests");
                sys.tests.automat.RunTests.run();
            }
            else if (args[0].compareTo("AutomatLogicTests") == 0)
            {
                System.out.println("Running automat logic tests");
                sys.tests.automatLogic.RunTests.run();
            }
        }
        else
        {
            // FOr checking in
            /*System.out.println("Hello from Java!");*/
            //Test t = new Test();
            //t.mes();
            //t.more();
            //t.mes();
            /*t.end();*/

           /* try*/
            //{
                //JSONMessage mes = new JSONMessage("systema@localhost.localdomain", "systemb@localhost.localdomain", "subj");
                //mes.put("key1", 1);
                //new MessageSenderSMTP("localhost.localdomain").send(mes);

                //MessageReceiver mr = new MessageReceiverPOP3("localhost.localdomain", "systemb", "systemb");
                //MessageCheckerClient mcc = new T();
                //MessageChecker mc = new MessageChecker(mr, mcc, 1000);
                //mc.start();

                //Thread.sleep(4000);

                //mes = new JSONMessage("systema@localhost.localdomain", "systemb@localhost.localdomain", "subj");
                //mes.put("key1", 2);
                //new MessageSenderSMTP("localhost.localdomain").send(mes);

                //Thread.sleep(3000);

                //mc.stop();

                //TimerClient tc = new TimerClientClass();
                //sys.timer.Timer mytimer = new sys.timer.Timer();
                //mytimer.init(tc, 4, 2000, 5);
                //mytimer.start();
            //}
            //catch(Exception e)
            //{
                //System.out.println(e.getMessage());
            /*}*/

            /*DBWrapper dbw = new DBWrapper("localhost", "systema", "systema", "systema");*/
            //dbw.connect();
            ////dbw.test();
        //dbw.insert("Request", "ID, PayingSystem_Address, User_Name, Quantity", 0 + "," + "'ps_1'" + "," +  "'user_1'" + "," + 10);
            /*dbw.disconnect();*/

            /*SysaDatabase.dropTables("localhost", "systema", "systema", "systema");*/
            /*SysaDatabase.createTables("localhost", "systema", "systema", "systema");*/

            /*try*/
            //{
                //SysaDatabase.maxRequestID("localhost", "systema", "systema", "systema");
            //}
            //catch(Exception e)
            //{
                //System.out.println("Error when retrieving max request number : " + e.getMessage());
            /*}*/
             
            try
            {
                JSONMessage mes = new JSONMessage("xelax-b1@dev.iu7.bmstu.ru", "xelax-a1@dev.iu7.bmstu.ru", "shaman");
                //mes.put("Type", "newRequest");
                //mes.put("ID", 80);
                //mes.put("Quantity", 800);
                //mes.put("PayingSystem", "xelax-c1@dev.iu7.bmstu.ru");
                //mes.put("UserName", "user_2");
                /*new MessageSenderSMTP("dev.iu7.bmstu.ru", "10025").send(mes);*/
                
                mes.put("type", "shaman");
                new MessageSenderSMTP("dev.iu7.bmstu.ru", "10025").send(mes);

/*                ConcurrentLinkedQueue<UniMessage> messages = new MessageReceiverPOP3("dev.iu7.bmstu.ru", "10110", "xelax-a1", "jdi27b2c").receive();*/
                //for (UniMessage m : messages)
                //{
                    //System.out.println("FROM: " + m.getFrom() + ", TO: " + m.getTo() + ", BODY: " + m.getBody());
                /*}*/
           }
            catch(Exception e) {System.out.println(e.getMessage());}

            RequestManager rm = new RequestManager("configS");
            rm.start();
        }
    }

/*    public void testTimer()*/
    //{
        //timer = new Timer();
        //timer.schedule(new ToDo(), 1000, 2000); 
        //System.out.println("Waiting...");
    //}

    //class ToDo extends TimerTask
    //{
        //public void run()
        //{
            //System.out.println("From timer");
            ////timer.cancel();    
        //}
    /*}*/
}

/*class TimerClientClass implements TimerClient*/
//{
    //public void timeout(Object marker)
    //{
        //System.out.println(marker.toString());
    //}
//}

//class T implements MessageCheckerClient
//{
    //public void newMessages(Vector<UniMessage> messages)
    //{
        //for (UniMessage m : messages)
        //{
            //System.out.println("In message checker client");
            //System.out.println("FROM: " + m.getFrom() + ", TO: " + m.getTo() + ", BODY: " + m.getBody());
        //}
    //}
/*}*/
